﻿//打印购物车
function PrintCart(jsonData) {
    var $p = $(".mall_menu_cart");
    $("#mall_menu_cart_container .delete").remove();
    if ($p.length > 0 && jsonData.Attr.length > 0) {
        //TODO 更新全局购物车
        var list = jsonData.Attr;
        var total = 0;
        if (list == null) return;
        for (var i = 0; i < list.length; i++) {
            var model = list[i];
            var count = model.Count;
            total += count;
        }
        $("#mall_menu_cart_count").text(total);
        $("#mall_menu_cart_container").SetHtmlData(list);
    } else {
        //location.reload();
        $("#mall_menu_cart_count").text(0);
        $("#mall_menu_cart_container").append("<li class='fc2 tc delete' style='width:100%'>购物车空空如也</li>");

    }
}

//获取购物车信息
function GetCart(fn) {
    $.ajaxc("/Cart/GetCart", {}, function (data) {
        if (fn) fn(data);
        PrintCart(data);
    });
}

//加入购物车
function AddCart(option, fn) {
    //  option  =  { pId: option.pId, count: option.count }
    $.ajaxc("/Cart/AddCart", option, function (data) {
        if (fn) fn(data);
        PrintCart(data);
    });
}

//清空购物车
function ClearCart(fn) {
    msgcon({
        message: "您确定要清空购物车吗？",
        yesTxt: "清空",
        noTxt: "关闭",
        yesFn: function () {
            $.ajaxc("/Cart/ClearCart", {}, function (data) {
                if (fn) fn(data);
            });
        },
        noFn: function () {

        }
    });

}

function RemoveCart(option, fn) {
    msgcon({
        message: "您确定要删除此商品吗？",
        yesTxt: "删除",
        noTxt: "关闭",
        yesFn: function () {
            $.ajaxc("/Cart/RemoveCart/" + option.id, {}, function (data) {
                $("#cart_info_" + option.id).remove();
                if (location.href.toLowerCase().indexOf("/cart/index") > 0) {
                    //TODO 购物车首页则直接刷新
                    location.reload();
                }
                if (fn) fn(data);
            });
        },
        noFn: function () {

        }
    });

}

//更新首页购物车
function UpdateIndexCart() {

}